<template>
	<view class="container">
		<view class="message-list">
			<view class="message-item" v-for="(item, index) in messageList" :key="index" @click="openMessage(item)">
				<view class="message-avatar">
					<image :src="item.avatar"></image>
					<view class="badge" v-if="item.unread > 0">{{item.unread}}</view>
				</view>
				<view class="message-content">
					<view class="message-header">
						<text class="message-title">{{item.title}}</text>
						<text class="message-time">{{item.time}}</text>
					</view>
					<view class="message-desc">{{item.content}}</view>
				</view>
			</view>
		</view>
		
		<view class="section-title">我的聊天</view>
		<view class="message-list">
			<view class="message-item" v-for="(item, index) in chatRoomList" :key="index" @click="openChatRoom(item)">
				<view class="message-avatar">
					<image :src="item.avatar || '/static/default-avatar.png'"></image>
					<view class="badge" v-if="item.unreadCount > 0">{{item.unreadCount}}</view>
				</view>
				<view class="message-content">
					<view class="message-header">
						<text class="message-title">{{item.roomName}}</text>
						<text class="message-time">{{item.lastMessageTime}}</text>
					</view>
					<view class="message-desc">{{item.lastMessage || '暂无消息'}}</view>
				</view>
				<view class="room-type-tag" v-if="item.roomType === '1'">群聊</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				messageList: [
					{
						id: 1,
						title: '系统通知',
						content: '您的账户已激活，可以开始使用所有功能',
						time: '12:30',
						avatar: '/static/icon-system.png',
						unread: 1
					},
					{
						id: 2,
						title: '客服消息',
						content: '有任何问题请随时联系我们的客服团队',
						time: '昨天',
						avatar: '/static/icon-service.png',
						unread: 0
					},
					{
						id: 3,
						title: '订单通知',
						content: '您购买的彩票预测服务已生效',
						time: '前天',
						avatar: '/static/icon-order.png',
						unread: 2
					}
				],
				chatRoomList: []
			}
		},
		onShow() {
			// 页面显示时获取最新的聊天室列表
			this.getChatRoomList();
		},
		methods: {
			openMessage(item) {
				// 打开消息详情
				uni.showToast({
					title: '打开消息：' + item.title,
					icon: 'none'
				})
			},
			getChatRoomList() {
				// 获取聊天室列表
				const token = uni.getStorageSync('loginToken');
				const baseUrl = uni.$baseUrl || 'http://localhost:8080';
				uni.request({
					url: baseUrl + '/api/chat/rooms',
					method: 'GET',
					header: {
						'Authorization': `Bearer ${token}`
					},
					success: (res) => {
						if (res.data.code === 200) {
							this.chatRoomList = res.data.data.map(item => {
								// 格式化时间
								let lastTime = item.lastMessageTime ? this.formatTime(new Date(item.lastMessageTime)) : '';
								return {
									...item,
									lastMessageTime: lastTime,
									avatar: item.roomType === '1' ? '/static/icon-group.png' : '/static/icon-user.png'
								};
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '获取聊天列表失败',
							icon: 'none'
						});
					}
				});
			},
			openChatRoom(item) {
				// 打开聊天室
				uni.navigateTo({
					url: `/pages/chat/chat?roomId=${item.roomId}&roomName=${item.roomName}&roomType=${item.roomType}`
				});
			},
			formatTime(date) {
				const now = new Date();
				const diff = now.getTime() - date.getTime();
				const day = 24 * 60 * 60 * 1000;
				
				if (diff < day) {
					return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
				} else if (diff < 2 * day) {
					return '昨天';
				} else {
					return `${date.getMonth() + 1}-${date.getDate()}`;
				}
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 20rpx;
		box-sizing: border-box;
	}
	
	.message-list {
		background-color: #fff;
		border-radius: 10rpx;
		margin-bottom: 30rpx;
	}
	
	.message-item {
		display: flex;
		padding: 30rpx 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
		position: relative;
	}
	
	.message-avatar {
		position: relative;
		width: 80rpx;
		height: 80rpx;
		margin-right: 20rpx;
	}
	
	.message-avatar image {
		width: 100%;
		height: 100%;
		border-radius: 40rpx;
	}
	
	.badge {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		background-color: #FF0000;
		color: #fff;
		font-size: 20rpx;
		min-width: 32rpx;
		height: 32rpx;
		line-height: 32rpx;
		text-align: center;
		border-radius: 16rpx;
		padding: 0 6rpx;
	}
	
	.message-content {
		flex: 1;
	}
	
	.message-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10rpx;
	}
	
	.message-title {
		font-size: 30rpx;
		font-weight: bold;
	}
	
	.message-time {
		font-size: 24rpx;
		color: #999;
	}
	
	.message-desc {
		font-size: 26rpx;
		color: #666;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		padding: 20rpx 10rpx;
	}
	
	.room-type-tag {
		position: absolute;
		top: 30rpx;
		right: 20rpx;
		font-size: 22rpx;
		color: #fff;
		background-color: #1296db;
		padding: 2rpx 12rpx;
		border-radius: 10rpx;
	}
</style> 